/**
 * 功能:生产计划单 控制器类
 * 开发人员:chengd
 * 创建时间:2012/5/12 18:06:10
 */
package com.towker.frame.controller.sell;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.towker.web.MyRequest;
import com.towker.core.page.PageModel;
import com.towker.core.page.PageNav;
import com.towker.core.util.CollectionUtil;
import com.towker.core.util.RefGoodsType;
import com.towker.core.util.StringUtil;
import com.towker.frame.controller.BaseController;
import com.towker.frame.model.sell.SellPlanApp;
import com.towker.frame.service.sell.SellPlanAppService;
import com.towker.frame.share.sys.user.UserShare;

//生产计划单
@Controller
@SuppressWarnings("unchecked")
@RequestMapping("/manage/sell")
public class SellPlanAppController extends BaseController
{
	private SellPlanAppService service;

	//业务对象注入
	public void setSellPlanAppService(SellPlanAppService service)
	{
		this.service = service;
	}

	//添加
	@RequestMapping("/addSellPlanApp{viewName}")
	public ModelAndView addSellPlanApp(HttpServletRequest request, HttpServletResponse response, SellPlanApp po, @PathVariable String viewName) throws IOException
	{
		MyRequest myRequest = new MyRequest(request);
		ModelAndView result = null;
		if ("1".equals(viewName))
		{
			result = new ModelAndView();
			result.addObject("app_code", service.getNewAppCode());
			result.setViewName("/manage/sell/addSellPlanApp.jsp");
		}
		else if ("2".equals(viewName))
		{
			PrintWriter writer = response.getWriter();
			try
			{
				String goodsInfo = myRequest.getString("goodsInfo");
				po.setCreate_time(new Date());
				po.setCreater_code(UserShare.getCurrentUserCode(request));
				po.setCreater_id(new Integer(String.valueOf(UserShare.getCurrentUserId(request))));
				po.setApp_code(service.getNewAppCode());
				
				po.setAuthCoCode(UserShare.getCurrentUserCoCode(request));
				//po.setAuthDepCode(UserShare.getCurrentUserDepCode(request));
				
				service.add(po);
				processRefGoods(request, RefGoodsType.SELL_PLAN_APP_GOODS, po.getId(), goodsInfo);
				writer.write("添加生产计划单成功！");
			}
			catch (Exception e)
			{
				e.printStackTrace();
				writer.write("添加生产计划单失败！" + e.getCause());
			}
		}
		return result;
	}

	//删除
	@RequestMapping("/delSellPlanApp")
	public void delSellPlanApp(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		MyRequest req = new MyRequest(request);
		Integer[] ids = CollectionUtil.toIntegerArray(req.getIntArray("keyIndex", 0));
		for (Integer id : ids) {
			service.delById(id);
			refGoodsService.deleteByInfoType(RefGoodsType.SELL_PLAN_APP_GOODS.getValue(), id);
		}
		response.sendRedirect(req.getRefererURL());
	}

	//修改
	@RequestMapping("/updSellPlanApp{viewName}")
	public ModelAndView updSellPlanApp(HttpServletRequest request, HttpServletResponse response, SellPlanApp po, @PathVariable String viewName) throws IOException
	{
		ModelAndView result = null;
		MyRequest req = new MyRequest(request);
		String goodsInfo = req.getString("goodsInfo");
		if ("1,3,5".indexOf(viewName) > -1)
		{
			Integer id = req.getInt("keyIndex");
			po = service.getById(id);
			result = new ModelAndView();
			result.addObject("po", po);
			result.addObject("action", StringUtil.toInteger(viewName));
			result.addObject("refGoods", refGoodsService.getByInfoType(RefGoodsType.SELL_PLAN_APP_GOODS.getValue(), id));
			result.setViewName("/manage/sell/updSellPlanApp.jsp");
		}
		else if ("2".equals(viewName))
		{
			PrintWriter writer = response.getWriter();
			try
			{
				SellPlanApp oldApp = service.getById(po.getId());
				oldApp.setCustom_code(po.getCustom_code());
				oldApp.setCreate_date(po.getCreate_date());
				oldApp.setApp_type(po.getApp_type());
				oldApp.setRemark(po.getRemark());
				service.update(oldApp);
				processRefGoods(request, RefGoodsType.SELL_PLAN_APP_GOODS, po.getId(), goodsInfo);
				writer.write("修改生产计划单成功！");
			}
			catch (Exception e)
			{
				e.printStackTrace();
				writer.write("修改生产计划单失败！" + e.getCause());
			}
		} else if ("4".equals(viewName)) {
			PrintWriter writer = response.getWriter();
			try
			{
				SellPlanApp oldApp = service.getById(po.getId());
				oldApp.setSubmit_date(po.getSubmit_date());
				oldApp.setSubmit_time(new Date());
				oldApp.setRemark(po.getRemark());
				oldApp.setStatus(1);
				service.update(oldApp);
				writer.write("提交生产计划单成功！");
			}
			catch (Exception e)
			{
				e.printStackTrace();
				writer.write("提交生产计划单失败！" + e.getCause());
			}
		}else if ("6".equals(viewName)) {
			PrintWriter writer = response.getWriter();
			try
			{
				SellPlanApp oldApp = service.getById(po.getId());
				oldApp.setAudit_user_code(UserShare.getCurrentUserCode(request));
				oldApp.setAudit_user_id(UserShare.getCurrentUserId(request).intValue());
				oldApp.setPro_date(po.getPro_date());
				oldApp.setPro_time(new Date());
				oldApp.setCom_date(po.getCom_date());
				oldApp.setCom_time(new Date());
				oldApp.setSh_remark(po.getSh_remark());
				oldApp.setPro_remark(po.getPro_remark());
				oldApp.setCom_remark(po.getCom_remark());
				oldApp.setStatus(2);
				service.update(oldApp);
				writer.write("审核生产计划单成功！");
			}
			catch (Exception e)
			{
				e.printStackTrace();
				writer.write("审核生产计划单失败！" + e.getCause());
			}
		}
		return result;
	}

	//获得分页
	@RequestMapping("/getSellPlanApp")
	public ModelAndView getSellPlanApp(HttpServletRequest request,HttpServletResponse response, @RequestParam(value="page", defaultValue="1")int page)
	{
		int pageSize = 10;
		MyRequest req = new MyRequest(request);
		Map map = req.getParameterValueMap(false, true);
		//只查询当前用户所属部门
		map.put("authCoCode", UserShare.getCurrentUserCoCode(request));
		//map.put("authDepCode", UserShare.getCurrentUserDepCode(request));
		PageModel<SellPlanApp> pageModel = service.getPage(page, pageSize, map);
		ModelAndView result = new ModelAndView();
		PageNav pageNav = new PageNav(request, pageModel);
		result.addObject("pageModel", pageModel);
		result.addObject("pageNav", pageNav);
		result.setViewName("/manage/sell/getSellPlanApp.jsp");
		return result;
	}

	//明细
	@RequestMapping("/getSellPlanAppById")
	public ModelAndView getSellPlanAppById(HttpServletRequest request, HttpServletResponse response)
	{
		SellPlanApp po = null;
		MyRequest req = new MyRequest(request);
		Integer id = req.getInt("keyIndex");
		po = service.getById(id);
		ModelAndView result = new ModelAndView();
		result.addObject("po", po);
		result.addObject("refGoods", refGoodsService.getByInfoType(RefGoodsType.SELL_PLAN_APP_GOODS.getValue(), id));
		result.setViewName("/manage/sell/getSellPlanAppById.jsp");
		return result;
	}
}
